<?php
function showPID()
{
    $pid = posix_getpid();
    fprintf(STDOUT,"pid=%d,ppid=%d,pgid=%d,sid=%d\n",
    $pid,posix_getppid(),posix_getpgid($pid),posix_getsid($pid));
}

showPID();
$pidMap = array();
$pid = pcntl_fork();

if ($pid>0){
    $pidMap[$pid] = $pid;
    $pid = pcntl_fork();
    if ($pid>0){
        $pidMap[$pid] = $pid;
    }else{
       $pid = posix_getpid();
       posix_setpgid($pid,$pid);

       $pid = pcntl_fork();
       if ($pid>0){
           $pidMap[$pid] = $pid;
       }
    }
}
showPID();
if ($pid>0){
    $i=0;
    while (1){
       $pid = pcntl_waitpid(-1,$status);
       if ($pid>0){
           $i++;
           echo "子进程".$pid."结束了\n";
       }
       unset($pidMap[$pid]);
       if (empty($pidMap)){
           break;
       }
    }
}


